/**
 * [4] 寻找两个正序数组的中位数
 *
 * @format
 * @lc app=leetcode.cn id=4 lang=javascript
 */

// @lc code=start
/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number}
 */
var findMedianSortedArrays = function (nums1, nums2) {
    let [index1, index2] = [0, 0]
    let num1, num2
    const merge = []
    while (nums1[index1] !== undefined || nums2[index2] !== undefined) {
        ;[num1, num2] = [nums1[index1], nums2[index2]]

        if (num1 < num2 || num2 === undefined) {
            merge.push(num1)
            index1++
        } else {
            merge.push(num2)
            index2++
        }
    }
    index1 = merge.length / 2
    return merge.length % 2 === 1
        ? merge[parseInt(index1)]
        : (merge[index1] + merge[index1 - 1]) / 2
}
// @lc code=end
